package com.example.Lezedata.ui.activity.record;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.example.Lezedata.R;
import com.example.Lezedata.dataModel.BidRecordModel;
import com.example.Lezedata.net.GBError;
import com.example.Lezedata.net.GBNetRequest;
import com.example.Lezedata.net.ServerResponseObserver;
import com.example.Lezedata.ui.activity.award.ReceiveAwardVc;
import com.example.Lezedata.ui.activity.baseClass.GBBaseVc;
import com.example.Lezedata.ui.activity.baseClass.GBListViewNoDataBaseAdapter;
import com.example.Lezedata.util.GBUtil;

import java.util.ArrayList;

import bid.Bid;
import common.Common;

public class PrizesRecord extends GBBaseVc {
    @Override
    protected int getLayoutId() {
        return R.layout.activity_prizes_record;
    }

    @Override
    public CharSequence title() {
        return "My Prizes";
    }
    ArrayList<Bid.UserWinPrizeListResponse.UserWinPrizeItem> models = new ArrayList<>();

    private int pageStart = 0;
    private int pageSize = 20;

    ListView listView;
    @Override
    protected void viewDidLoad() {
        super.viewDidLoad();
        listView = findViewById(R.id.BidRecord_list);
        setAdapter();
        addLineToHeaderFooter();
        loadData();
    }

    void addLineToHeaderFooter(){
        TextView line = new TextView(mContext);
        line.setMaxHeight(GBUtil.pxTo2dp(mContext,15));
        line.setBackground(new ColorDrawable(Color.TRANSPARENT));
        listView.addHeaderView(line);

        TextView line1 = new TextView(mContext);
        line1.setMaxHeight(GBUtil.pxTo2dp(mContext,15));
        line1.setBackground(new ColorDrawable(Color.TRANSPARENT));
        listView.addFooterView(line1);
    }

    GBListViewNoDataBaseAdapter adapter;
    void setAdapter(){
        adapter = new GBListViewNoDataBaseAdapter(listView) {
            @Override
            public int getCount() {
                return models.size();
            }

            @Override
            public Bid.UserWinPrizeListResponse.UserWinPrizeItem getItem(int position) {
                return models.get(position);
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                if (convertView == null){
                    convertView = PrizesRecordCell.sharedCell(mContext);
                }
                PrizesRecordCell cell = (PrizesRecordCell)convertView;
                cell.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        gotoReceiveAward();
                    }
                });
                cell.setModel(getItem(position));
                return convertView;
            }
        };
        listView.setAdapter(adapter);
    }

    void loadData(){
        Common.PageInfo pageInfo = Common.PageInfo.newBuilder().setPageSize(pageSize).setPageStart(pageStart).build();
        GBNetRequest.stubUser().userWinPrizeList(pageInfo, ServerResponseObserver.observer(new ServerResponseObserver.requestListener<Bid.UserWinPrizeListResponse>() {
            @Override
            public void complete(Bid.UserWinPrizeListResponse value, GBError error) {
                if (error == null){
                    if (pageStart == 0){
                        models.clear();
                    }
                    models.addAll(value.getListList());
                }
                adapter.notifyDataSetChanged();
            }
        }));
    }

    void gotoReceiveAward(){
        jumpActivity(ReceiveAwardVc.class);
    }
}